package com.skytech.component.crawler.parse;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

public class SeleniumDownloader implements HtmlDownloader {

    private String driverPath;
    public SeleniumDownloader(String driverPath){
        this.driverPath=driverPath;
    }

    @Override
    public Document download(String url) throws Exception {
        System.setProperty("webdriver.chrome.driver", driverPath);//加载驱动
        /*ChromeDriver driver;*/
        ChromeOptions options = new ChromeOptions();

        options.addArguments("--headless");//为了程序在Linux环境无可视化界面情况下运行
        options.addArguments("--no-sandbox");//为了程序在Linux环境无可视化界面情况下运行

        ChromeDriver driver=new ChromeDriver(options);
        driver.get(url);
        //Thread.sleep(1000);
        String htmlNew = driver.findElementByTagName("html").getAttribute("outerHTML");

        Document parse = Jsoup.parse(htmlNew, driver.getCurrentUrl());
        driver.quit();
        return parse;
    }
}
